package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysWarehouseDao;
import com.gusha.model.SysTraining;
import com.gusha.model.SysWarehouse;
import com.gusha.model.TrainComm;
import com.gusha.model.User;

@Service
@Transactional
public class SysWarehouseService {
@Autowired
private SysWarehouseDao sysWarehouseDao;


public Page<SysWarehouse> findAll(Pageable pageable, SysWarehouse sysWarehouse) {
    @SuppressWarnings("serial")
    Page<SysWarehouse> result = sysWarehouseDao.findAll(new Specification<SysWarehouse>() {
        @Override
        public Predicate toPredicate(Root<SysWarehouse> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = new ArrayList<Predicate>();
            if (sysWarehouse.getId() != null) {
                list.add(cb.equal(root.get("id").as(BigInteger.class),sysWarehouse.getId()));
            }
            if (sysWarehouse.getUser() != null) {
                list.add(cb.equal(root.get("user").as(User.class),sysWarehouse.getUser()));
            }
            if (sysWarehouse.getWarehouseNo() != null) {
                list.add(cb.equal(root.get("warehouseNo").as(BigInteger.class),sysWarehouse.getWarehouseNo()));
            }
            if (sysWarehouse.getVegetablesNum() != null) {
                list.add(cb.equal(root.get("vegetablesNum").as(BigInteger.class),sysWarehouse.getVegetablesNum()));
            }
            if (sysWarehouse.getStealVegetablesNum() != null) {
                list.add(cb.equal(root.get("stealVegetablesNum").as(BigInteger.class),sysWarehouse.getStealVegetablesNum()));
            }
            if (sysWarehouse.getFruitsNum() != null) {
                list.add(cb.equal(root.get("fruitsNum").as(BigInteger.class),sysWarehouse.getFruitsNum()));
            }
            if (sysWarehouse.getStealFruitsNum() != null) {
                list.add(cb.equal(root.get("stealFruitsNum").as(BigInteger.class),sysWarehouse.getStealFruitsNum()));
            }
            if (sysWarehouse.getPoultryNum() != null) {
                list.add(cb.equal(root.get("poultryNum").as(BigInteger.class),sysWarehouse.getPoultryNum()));
            }
            if (sysWarehouse.getStealPoultryNum() != null) {
                list.add(cb.equal(root.get("stealPoultryNum").as(BigInteger.class),sysWarehouse.getStealPoultryNum()));
            }
            if (sysWarehouse.getFlowersNum() != null) {
                list.add(cb.equal(root.get("flowersNum").as(BigInteger.class),sysWarehouse.getFlowersNum()));
            }
            if (sysWarehouse.getStealFlowersNum() != null) {
                list.add(cb.equal(root.get("stealFlowersNum").as(BigInteger.class),sysWarehouse.getStealFlowersNum()));
            }
            if (sysWarehouse.getAquaticNum() != null) {
                list.add(cb.equal(root.get("aquaticNum").as(BigInteger.class),sysWarehouse.getAquaticNum()));
            }
            if (sysWarehouse.getStealAquaticNum() != null) {
                list.add(cb.equal(root.get("stealAquaticNum").as(BigInteger.class),sysWarehouse.getStealAquaticNum()));
            }
            if (sysWarehouse.getMoney() != null) {
                list.add(cb.equal(root.get("money").as(BigDecimal.class),sysWarehouse.getMoney()));
            }
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }
    },pageable);
     return result;
}
}
